<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <search-bar-new :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :show-summary="true"
          :summary-method="getSummaries"
          :header-cell-class-name="cellClassName"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" align="center" width="70" />
          <el-table-column
            prop="deviceCodeName"
            label="设备信息"
            :show-overflow-tooltip="true"
            min-width="200"
          />
          <el-table-column
            prop="deviceModel"
            label="规格型号"
            :show-overflow-tooltip="true"
            min-width="120"
          />
          <el-table-column
            prop="deviceLocation"
            label="设备位置"
            :show-overflow-tooltip="true"
            min-width="120"
          />
          <el-table-column
            prop="useRate"
            label="设备利用率%"
            :min-width="SETTINGS.settings_decimalDigits_length"
          />
          <el-table-column
            label="备品备件"
          >
            <el-table-column
              prop="spareChangeNumber"
              label="更换数量"
              :min-width="SETTINGS.settings_decimalDigits_length"
            />
            <el-table-column
              align="right"
              prop="spareChangePriceThousandths"
              label="更换金额(元)"
              :min-width="SETTINGS.settings_decimalDigits_length"
            />
          </el-table-column>

          <el-table-column
            label="维修"
          >
            <el-table-column
              prop="repairTimes"
              label="维修次数"
              min-width="100"
            />
            <el-table-column
              prop="repairByOneselfTimes"
              label="自修次数"
              min-width="100"
            />
            <el-table-column
              prop="repairByOutTimes"
              label="委外次数"
              min-width="100"
            />
            <el-table-column
              align="right"
              prop="repairMoneyThousandths"
              label="维修费用(元)"
              :min-width="SETTINGS.settings_decimalDigits_length"
            />
            <el-table-column
              prop="duration"
              label="维修时长"
              :min-width="SETTINGS.settings_decimalDigits_length"
            />
          </el-table-column>

          <el-table-column
            label="保养"
          >
            <el-table-column
              prop="maintainTimes"
              label="保养次数"
              min-width="100"
            />
            <el-table-column
              prop="maintainHours"
              label="保养工时(h)"
              :min-width="SETTINGS.settings_decimalDigits_length"
            />
            <el-table-column
              prop="maintainCompletedRatio"
              label="保养完成率%"
              :min-width="SETTINGS.settings_decimalDigits_length"
            />
          </el-table-column>
          <el-table-column
            label="点检"
          >
            <el-table-column
              prop="checkTimes"
              label="点检次数"
              min-width="100"
            />
            <el-table-column
              prop="exceptionTimes"
              label="异常次数"
              min-width="100"
            />
            <el-table-column
              prop="checkCompletedRatio"
              label="点检完成率%"
              :min-width="SETTINGS.settings_decimalDigits_length"
            />
            <el-table-column
              prop="cost"
              label="点检工时"
              :min-width="SETTINGS.settings_decimalDigits_length"
            />
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { list } from '@/api/equipmentManage/equipmentStatement/maintenanceReport'
import { getEquipmentList1 } from '@/api/equipmentManage/baseInfo/equipmentInfo'
import { getBlob } from '@/utils/util'
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '设备维保统计',
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      isManyhead: true,
      total: 1, // 数据一共有多少条
      mainId: 'id',
      tableHeight: '0',
      tableData: [],
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      pushColumns: [], // 推送配置实际用的字段参数
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'searchValue', label: '模糊搜索', type: 'input' },
        { prop: 'bookId', label: '设备信息', type: 'select', optionsName: 'bookList' },
        // { prop: 'deviceModel', label: '规格型号', type: 'input' },
        // { prop: 'deviceLocation', label: '设备位置', type: 'input' },
        { prop: 'searchTime', label: '统计时间', type: 'time', hasDefault: true, defaultKey: 'defaultSearchTime' }
      ],
      defaultPushColumns: [// 推送配置常规字段（初始值）
        { prop: 'pushCycle', label: '推送周期', type: 'select', optionsName: 'pushCycleList' },
        { prop: 'sendUsers', label: '推送对象', type: 'pushUser-select', optionsName: 'pushUserList' },
        { prop: 'cancelPush', label: '是否启用', type: 'switch' },
        { prop: '', label: '筛选范围', type: '' },
        { prop: 'bookId', label: '设备信息', type: 'select', optionsName: 'bookList' },
        { prop: 'deviceModel', label: '规格型号', type: 'input' },
        { prop: 'deviceLocation', label: '设备位置', type: 'input' },
        { prop: 'searchTime', label: '统计时间', type: 'time-select', hasDefault: true, defaultKey: 'defaultSearchTime' }
      ],
      pushCycleList: [
        { value: '每天', label: '每天' },
        { value: '每周一', label: '每周一' },
        { value: '每月一号', label: '每月一号' }
      ],
      pushUserList: [],
      multiHeader: [
        ['设备信息', '规格型号', '设备位置', '设备利用率%', '备品备件', '', '维修', '', '', '', '', '保养', '', '', '点检', '', '', '']
      ],
      header: [
        '', '', '', '', '更换数量', '更换金额(元)', '维修次数', '自修次数', '委外次数', '维修费用(元)', '维修时长', '保养次数', '保养工时(h)', '保养完成率%', '点检次数', '异常次数', '点检完成率%', '点检工时'
      ],
      keyWord: [
        'deviceCodeName', 'deviceModel', 'deviceLocation', 'useRate', 'spareChangeNumber', 'spareChangePrice', 'repairTimes', 'repairByOneselfTimes', 'repairByOutTimes', 'repairMoney', 'duration', 'maintainTimes', 'maintainHours', 'maintainCompletedRatio', 'checkTimes', 'exceptionTimes', 'checkCompletedRatio', 'cost'
      ],
      merges: [
        'A1:A2', 'B1:B2', 'C1:C2', 'D1:D2', 'E1:F1', 'G1:K1', 'L1:N1', 'O1:R1'
      ],
      headerStyle: [
        'A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1', 'I1', 'J1', 'K1', 'L1', 'M1', 'N1', 'O1', 'P1', 'Q1', 'R1'
      ],
      defaultTableColumns: [],
      operationData: [ // 右上角的操作按钮
        // { label: '新增', perm: '' },
        // { label: '批量删除', perm: '' },
        { label: '批量导出', perm: '' },
        { label: '页面配置', perm: '' }
      ],
      statusOptions: [],
      statusOptionsWarehouse: [],
      bookList: [],
      totalData: {},
      defaultSearchTime: []
    }
  },
  computed: {
    // 分页器
    selectedNum() {
      return this.$refs.table ? this.$refs.table.selection.length : 0
    }
  },
  created() {
    this.defaultSearchTime = [this.$moment(new Date()).subtract(1, 'months').format('YYYY-MM-DD') + ' 00:00:00',
      this.$moment(new Date()).format('YYYY-MM-DD') + ' 23:59:59']
    this.getBookList()
  },
  methods: {
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data.dataTable.data
        this.total = res.data.dataTable.total
        this.totalData = res.data.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    getBookList() {
      getEquipmentList1({ status: 1 }).then(res => {
        res.data.forEach(item => {
          item['value'] = item.id
          item['label'] = item.code + '/' + item.name
        })
        res.data.unshift({ value: undefined, label: '全部' })
        this.bookList = res.data
      })
    },
    getExportData(data) {
      // return list(data)
      return getBlob('/device/repair/statistic/repairMaintainStatisticList/export', data, 'post')
    },
    // 合计行
    getSummaries(param) {
      const {
        columns
      } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index == 0) {
          sums[index] = '合计'
          return
        }
        switch (column.property) {
          case 'spareChangeNumber':
            sums[index] = this.totalData.spareChangeNumber
            break
        }
        switch (column.property) {
          case 'spareChangePrice':
            sums[index] = this.totalData.spareChangePrice
            break
        }
        switch (column.property) {
          case 'repairTimes':
            sums[index] = this.totalData.repairTimes
            break
        }
        switch (column.property) {
          case 'repairByOneselfTimes':
            sums[index] = this.totalData.repairByOneselfTimes
            break
        }
        switch (column.property) {
          case 'repairByOutTimes':
            sums[index] = this.totalData.repairByOutTimes
            break
        }
        switch (column.property) {
          case 'repairMoney':
            sums[index] = this.totalData.repairMoney
            break
        }
        switch (column.property) {
          case 'maintainTimes':
            sums[index] = this.totalData.maintainTimes
            break
        }
        switch (column.property) {
          case 'maintainHours':
            sums[index] = this.totalData.maintainHours
            break
        }
        switch (column.property) {
          case 'checkTimes':
            sums[index] = this.totalData.checkTimes
            break
        }
        switch (column.property) {
          case 'exceptionTimes':
            sums[index] = this.totalData.exceptionTimes
            break
        }
      })
      return sums
    },
    cellClassName(data) {
      if (data.column.label == '备品备件' || data.column.label == '维修' ||
        data.column.label == '保养' || data.column.label == '点检') {
        return 'center'
      }
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-table thead.is-group th.el-table__cell{
  background-color: transparent!important;
}
/deep/ .el-table__footer-wrapper tbody td.el-table__cell{
  background-color: rgba(0,0,0,0);
}
/deep/ .el-table__footer-wrapper tbody td.el-table__cell{
  background-color: transparent!important;
}
/deep/ .el-table th.el-table__cell.is-leaf, .el-table td.el-table__cell {
    border-bottom: 1px solid #dfe6ec;
}

/deep/ .el-table__header-wrapper .center .cell{
  justify-content: center !important;
}

/deep/ .el-table__header-wrapper{
  .el-table__cell{
    border-right: 1px solid #dfe6ec !important;
  }
  tr:nth-child(2){
    .el-table__cell{
      border-top: 1px solid #dfe6ec !important;
    }
  }
}
</style>
